package com.hxht.storage.server.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxht.storage.server.bean.PlanRecordWeekTemplateBean;
import com.hxht.storage.server.service.WeekTemplateService;

@Controller
@RequestMapping("/weekTemplate")
public class WeekTemplateController {
	
	@Autowired
	private WeekTemplateService templateService;
	
	@RequestMapping(value= "/", method = RequestMethod.POST)
	@ResponseBody
	public boolean addTemplate(@RequestBody PlanRecordWeekTemplateBean weekTemplate){
		
		return templateService.addWeekTemplate(weekTemplate);
	}

	/**
	 * 获得当前存储服务器管辖的所有NVR基本状态信息
	 * @return
	 */
	@RequestMapping(value= "/", method = RequestMethod.GET)
	@ResponseBody
	public List<PlanRecordWeekTemplateBean> getAllTemplate(){
		
		List<PlanRecordWeekTemplateBean> templateList = templateService.getAllWeekTemplate();
		
//		System.out.println(JSONObject.toJSONString(templateList));
		
		return templateList;
	}
	
	/**
	 * 根据计划模板的ID号，找到相应计划模板,并更新该模板
	 * @param nvr
	 */
	@RequestMapping(value= "/", method = RequestMethod.PUT)
	public void updateTemplate(@RequestBody PlanRecordWeekTemplateBean weekTemplate){
		
		templateService.updateWeekTemplate(weekTemplate);
	}
	
	/**
	 * 根据计划模板名，删除该模板
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value= "/", method = RequestMethod.DELETE)
	public void deleteTemplate(@RequestParam String templateName) throws UnsupportedEncodingException{
		
		
		templateName = new String(templateName.getBytes("ISO-8859-1"), "UTF-8");
		templateService.delWeekTemplate(templateName);
	}
	
}
